package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Created by Thinkpad on 2016/3/9.
 */
public class FriendsTogetherInfoss extends Activity implements View.OnClickListener {
    private HttpUtils httpUtils;
    private TextView my_nicknames_together;
    private TextView my_names_together;
    private RelativeLayout setting_info;
    private TextView my_tels_together;
    private TextView my_companys_together;
    private  TextView my_dutys_together;
    private Button my_messages_together;
    private ImageButton my_delete;

    private  Intent intent;
    private ImageButton  friends_back15;
    private String together;
    List<String> cityName = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_together_infoss);

        intent = getIntent();
        together = intent.getStringExtra("togethers");


        System.out.println("这是传递过来的id："+together);

        httpUtils = new HttpUtils();
        initData();
    }

    private void initData() {
        my_names_together = (TextView) findViewById(R.id.my_names_addnews);
        my_nicknames_together = (TextView) findViewById(R.id.my_nicknames_addnews);

        my_tels_together = (TextView) findViewById(R.id.my_tels_addnews);
        my_companys_together = (TextView) findViewById(R.id.my_companys_addnews);
        my_dutys_together = (TextView) findViewById(R.id.my_dutys_addnews);

        friends_back15= (ImageButton)findViewById(R.id.friends_back18);


        my_messages_together = (Button) findViewById(R.id.my_messages_addnews);

        //拿到这个id去好友列表遍历，如果有就发送消息，没有就显示加为好友。
        String dd = SharedPreferencesUtils.getString(this, "dd");
        Gson gson = new Gson();
        FriendsListData friendsListData = gson.fromJson(dd, FriendsListData.class);
        List<FriendsListData.Data> data = friendsListData.getData();

        for (int i = 0; i < data.size(); i++) {
            String ID = data.get(i).getFriends().getID();
            //id存数组
            cityName.add(ID);
        }

        initView(together);

        friends_back15.setOnClickListener(this);
        my_messages_together.setOnClickListener(this);
    }

    private void initView(String id4) {
        System.out.print("这是请求链接:" + Constans.GETUSERINFO_URL1+id4);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETUSERINFO_URL1 + id4, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                System.out.println("这是共同好友个人信息：" + result);
                Gson gson = new Gson();
                FriendsInfoData friendsInfoData = gson.fromJson(result, FriendsInfoData.class);

                my_names_together.setText(friendsInfoData.getRealname());
                ;//真实姓名
                my_nicknames_together.setText(friendsInfoData.getNickname());
                my_tels_together.setText(friendsInfoData.getPhone());
                my_companys_together.setText(friendsInfoData.getCompany());
                my_dutys_together = (TextView) findViewById(R.id.my_duty);

                for(int i=0;i<cityName.size();i++){
                    if(cityName.get(i).equals(friendsInfoData.getID())){
                        my_messages_together.setText("发送消息");
                        break;
                    }else{
                        my_messages_together.setText("加为好友");
                        break;
                    }

                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.friends_back15 :
                //关闭
                finish();
                break;


            case R.id.my_messages_together :

                if("加为好友".equals(my_messages_together.getText().toString())){

                //发送消息
                httpUtils.send(HttpRequest.HttpMethod.GET, Constans.ADDFRIENDS_URL +Constans.userID+ "&friendsID=" +together, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String s = responseInfo.result;
                        System.out.println("这是返回的值" + s);
                        //发送消息到主线程通知更改  加为好友为等待验证。
                        Utils.showToast("请求已经发送");
                        finish();

//
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                        Utils.showToast("网络获取异常");
                    }
                });
                }else{

                    if (RongIM.getInstance() != null) {
                        RongIM.getInstance().startPrivateChat(FriendsTogetherInfoss.this, together, "hello");
                }

                break;


        }

    }
}}
